package ru.alperez.model;

import org.json.JSONException;
import org.json.JSONObject;

public class ExtendedLocation extends android.location.Location{

	public ExtendedLocation(ExtendedLocation l) {
		super(l);
	}
	
	public ExtendedLocation(String provider) {
		super(provider);
	}
	
	public JSONObject toJSON() throws JSONException {
		JSONObject ret = new JSONObject();
		ret.put("accuracy", this.getAccuracy());
		ret.put("altitude", this.getAltitude());
		ret.put("bearing", this.getBearing());
		ret.put("lat", this.getLatitude());
		ret.put("lon", this.getLongitude());
		ret.put("provider", this.getProvider());
		ret.put("speed", this.getSpeed());
		ret.put("time", this.getTime());
		return ret;
	}
	
	public static ExtendedLocation fromJSON(JSONObject jLocation) {
		ExtendedLocation ret = null;
		try {
			ret = new ExtendedLocation(jLocation.optString("provider"));
			ret.setAccuracy((float) jLocation.optDouble("accuracy"));
			ret.setAltitude(jLocation.optDouble("altitude"));
			ret.setBearing((float) jLocation.optDouble("bearing"));
			ret.setLatitude(jLocation.optDouble("lat"));
			ret.setLongitude(jLocation.optDouble("lon"));
			ret.setSpeed((float) jLocation.optDouble("speed"));
			ret.setTime(jLocation.optLong("time"));
		} catch (Exception e) {
			ret = null;
		}
		return ret;
	}
}
